package com.hd123.rumba.gwt.widget.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.DelegatingChangeListenerCollection;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

public class LabeledTextArea extends HorizontalPanel implements KeyboardListener {
  private Label label;
  private TextArea textArea;
  private String caption;
  private boolean required = false;
  private boolean readOnly = false;

  public LabeledTextArea() {
    setSpacing(4);

    label = new Label();
    label.setWidth("100px");
    label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    add(label);
    setCellVerticalAlignment(label, HorizontalPanel.ALIGN_TOP);

    textArea = createInputBox();
    textArea.setHeight("50px");
    add(textArea);
    setCellVerticalAlignment(textArea, HorizontalPanel.ALIGN_MIDDLE);

    textArea.addKeyboardListener(this);
  }

  public LabeledTextArea(String caption) {
    this();
    setCaption(caption);
  }

  protected TextArea createInputBox() {
    return new TextArea();
  }

  public Label getLabel() {
    return label;
  }

  public TextArea getTextArea() {
    return textArea;
  }

  public void setText(String text) {
    getTextArea().setText(text);
  }

  public String getText() {
    return getTextArea().getText();
  }

  public void setCaption(String caption) {
    this.caption = caption;
    if (required && !readOnly)
      DOM.setInnerHTML(label.getElement(), this.caption + "<font color='red'>*</font>");
    else
      getLabel().setText(caption);
  }

  public String getCaption() {
    return caption;
  }

  public void setRequired(boolean required) {
    this.required = required;
    setCaption(caption);
  }

  public void setReadOnly(boolean b) {
    this.readOnly = b;
    setCaption(this.caption);
  }

  public void onKeyDown(Widget sender, char keyCode, int modifiers) {
    keyHandler(sender, keyCode, modifiers);
  }

  public void onKeyPress(Widget sender, char keyCode, int modifiers) {
    keyHandler(sender, keyCode, modifiers);
  }

  public void onKeyUp(Widget sender, char keyCode, int modifiers) {
    keyHandler(sender, keyCode, modifiers);
  }

  private void keyHandler(Widget sender, char keyCode, int modifiers) {
    if (readOnly && !Util.isNavKey(keyCode))
      getTextArea().cancelKey();
  }

  DelegatingChangeListenerCollection changeListeners;

  public void addChangeListener(ChangeListener listener) {
    if (changeListeners == null)
      changeListeners = new DelegatingChangeListenerCollection(this, getTextArea());
    changeListeners.add(listener);
  }

  public void removeChangeListener(ChangeListener listener) {
    if (changeListeners != null)
      changeListeners.remove(listener);
  }
}
